package com.jay.tools.utils;

import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.widget.Toast;

/**
 * author: tanjianpei
 * date: 2019/8/19 019 15:01
 * description: 土司
 */
public class ToastUtil {

    private static Toast sToast = null;

    public static void toast(Context context, String msg) {
        if (null == context) {
            return;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            if (sToast == null) {
                sToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
                sToast.setGravity(Gravity.CENTER, 0, 0);
                sToast.show();
                return;
            }
            sToast.setGravity(Gravity.CENTER, 0, 0);
            sToast.setDuration(Toast.LENGTH_SHORT);
            sToast.setText(msg);
            sToast.show();
        }
    }

    public static void toast(Context context, int stringId) {
        if (null == context) {
            return;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            Toast toast = Toast.makeText(context, stringId, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            if (sToast == null) {
                sToast = Toast.makeText(context.getApplicationContext(), stringId, Toast.LENGTH_SHORT);
                sToast.setGravity(Gravity.CENTER, 0, 0);
                sToast.show();
                return;
            }
            sToast.setDuration(Toast.LENGTH_SHORT);
            sToast.setText(stringId);
            sToast.setGravity(Gravity.CENTER, 0, 0);
            sToast.show();
        }
    }

    public static void toastLong(Context context, String msg) {
        if (null == context) {
            return;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            if (sToast == null) {
                sToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_LONG);
                sToast.setGravity(Gravity.CENTER, 0, 0);
                sToast.show();
                return;
            }
            sToast.setGravity(Gravity.CENTER, 0, 0);
            sToast.setDuration(Toast.LENGTH_SHORT);
            sToast.setText(msg);
            sToast.show();
        }
    }

    public static void toastLong(Context context, int stringId) {
        if (null == context) {
            return;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            Toast toast = Toast.makeText(context, stringId, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            if (sToast == null) {
                sToast = Toast.makeText(context.getApplicationContext(), stringId, Toast.LENGTH_LONG);
                sToast.setGravity(Gravity.CENTER, 0, 0);
                sToast.show();
                return;
            }
            sToast.setGravity(Gravity.CENTER, 0, 0);
            sToast.setDuration(Toast.LENGTH_SHORT);
            sToast.setText(stringId);
            sToast.show();
        }

    }

}
